package com.ehualu.impda.hadoop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

import java.io.File;
import java.io.IOException;

/**
 * @Author: 吴敬超
 * @Date: 2019/9/18 14:37
 */
public class readhdfs {

    private final static Log LOG = LogFactory.getLog(HdfsExample.class.getName());

    private static final String STORAGE_POLICY_HOT = "HOT";
    private static String PATH_TO_HDFS_SITE_XML = HdfsExample.class.getClassLoader().getResource("hdfs-site.xml")
            .getPath();
    private static String PATH_TO_CORE_SITE_XML = HdfsExample.class.getClassLoader().getResource("core-site.xml")
            .getPath();

    private static Configuration conf = null;

    private static String PRNCIPAL_NAME = "hdfsDeveloper";
    private static String PATH_TO_KEYTAB = HdfsExample.class.getClassLoader().getResource("user.keytab").getPath();
    private static String PATH_TO_KRB5_CONF = HdfsExample.class.getClassLoader().getResource("krb5.conf").getPath();

    //private static String PATH_TO_SMALL_SITE_XML = HdfsExample.class.getClassLoader().getResource("smallfs-site.xml")
    //.getPath();

//    private FileSystem fSystem; /* HDFS file system */

    public void write(String wrjson,String depath,String fipath) throws IOException {
        FileSystem fSystem = FileSystem.get(conf);
        String DEST_PATH=depath;
        String FILE_NAME=fipath;

        final String content = "hi, I am bigdata. It is successful if you can see me.";
        FSDataOutputStream out = null;
        try {
            out = fSystem.create(new Path(DEST_PATH + File.separator + FILE_NAME));
            out.write(content.getBytes());
            out.hsync();
            LOG.info("success to write.");
        } finally {
            // make sure the stream is closed finally.
            IOUtils.closeStream(out);
        }
    }
//
//    private void instanceBuild() throws IOException {
//        // get filesystem
//        // 一般情况下，FileSystem对象JVM里唯一，是线程安全的，这个实例可以一直用，不需要立马close。
//        // 注意：
//        // 若需要长期占用一个FileSystem对象的场景，可以给这个线程专门new一个FileSystem对象，但要注意资源管理，别导致泄露。
//        // 在此之前，需要先给conf加上：
//        // conf.setBoolean("fs.hdfs.impl.disable.cache",
//        // true);//表示重新new一个连接实例，不用缓存中的对象。
//        fSystem = FileSystem.get(conf);
//    }


    public static void main(String[] args) {

//        readhdfs rd=new readhdfs();
//        rd.write();


    }
}
